package sisdroid.framework.sensors;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import sisdroid.framework.model.*;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.*;
public class WifiSensor implements Sensor
{

	Context context;
	WifiManager manager;
	public WifiSensor( Context context )
	{
		this.context = context;
		manager = (WifiManager) context.getSystemService( Context.WIFI_SERVICE );
		manager.startScan( );
	}
	
	@Override
	public Set<Net> getNetIDS()
	{
		Set<Net> result = new HashSet<Net>();
		
		// In case scan was not successfully we just bail out with an empty array	 
		List<ScanResult> wifiresult = manager.getScanResults(); 
		
		ScanResult strongest = null;
		
		// Null result means no ScanResult possible (WiFi is most likely disconnected) 
		if ( wifiresult != null ) 
		{
			if ( wifiresult.size() > 0 )
			{
				strongest = wifiresult.get( 0 );
			}
			for ( ScanResult sr : wifiresult )
			{
				if ( strongest.level < sr.level ) 
				{
					strongest = sr;
				}
				//TODO: Should we include signal strength?
				result.add( new Net( "wifimac", sr.BSSID ) );
				result.add( new Net( "wifissid", sr.SSID ) );
			}
			
			if  ( strongest != null )
			{
				result.add( new Net("swifimac", strongest.BSSID) );
				result.add( new Net("swifissid", strongest.SSID) );
			}
		}
		return result;
	}

}
